package com.sms.fileupload;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sms.DB.ConnectDB;
import com.sms.DB.OperateDB;
import com.sms.project.PReport;

public class NewReport {
	// 添加新的项目时进行数据的插入，成功返回true
	public boolean insertData(String limit, String taskID, String fromID,
			List<String> toID, String content, List<Map> appendFile) {
		boolean isSuccess = true;

		String sql = "";
		Connection conn = ConnectDB.getConnection();
		boolean autoCommit;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交

			// 向taskreport表插入数据
			sql = "insert into taskreport values(null, ?, 0, 0, '0', ?, ?, ?, now(), ?, '0', '0')";
			String forbid = "";
			if (limit.equals("all")) {
				forbid = "0";
			} else {
				forbid = "1";
			}
			List<Object> tlist = new ArrayList<Object>();
			tlist.add(forbid);
			tlist.add(taskID);
			tlist.add(fromID);
			tlist.add(toID.size());
			tlist.add(content);
			OperateDB.setList(tlist);
			int tn = OperateDB.getGeneratedKey(sql);

			// System.out.println(tn);

			if (tn == 0) { // 插入taskreport不成功
				isSuccess = false;
			} else {
				sql = "update taskreport set rootid = " + tn
						+ " where t_reportid = " + tn;
				int un = OperateDB.update(sql);
				if (un > 0) {
					if (toID != null && toID.size() > 0) {
						for (int i = 0; i < toID.size(); i++) {
							String toid = toID.get(i);
							sql = "insert into taskmember values(?, ?)";
							List<Object> gmlist = new ArrayList<Object>();
							gmlist.add(tn);
							gmlist.add(toid);
							OperateDB.setList(gmlist);
							if (!OperateDB.insert(sql)) {
								isSuccess = false;
								break;
							}
						}
					} else {
						isSuccess = false;
					}

					if (isSuccess) {
						if (appendFile == null || appendFile.size() < 1) {// 没有添加附件时
							// System.out.println("没有附件");
							isSuccess = true;
						} else {// 有附件
							for (int i = 0; i < appendFile.size(); i++) {
								Map<String, String> map = new HashMap<String, String>();
								map = (Map<String, String>) appendFile.get(i);
								String fileName = map.get("fileName")
										.toString();
								String saveName = map.get("saveName")
										.toString();
								// 向appendinfo表插入数据
								sql = "insert into appendinfo values('任务报告', ?, ?, ?)";
								List<Object> alist = new ArrayList<Object>();
								alist.add(tn);
								alist.add(fileName);
								alist.add(saveName);

								OperateDB.setList(alist);
								if (OperateDB.insert(sql)) {// 插入附件表是否成功
									sql = "update taskreport set isappend = '1' where t_reportid = "
											+ tn;
									if (OperateDB.update(sql) > 0) {
										isSuccess = true;
									} else {
										isSuccess = false;
									}
								} else {
									isSuccess = false;
								}
							}// for结束
						}
					}

				} else {
					isSuccess = false;
				}

			}

			if (isSuccess) {
				conn.commit();// 手动提交
			} else {
				conn.rollback();
			}
			conn.setAutoCommit(autoCommit);// 恢复现场
		} catch (SQLException e) {
			e.printStackTrace();
			isSuccess = false;
		} finally {
			OperateDB.close(conn);
		}

		return isSuccess;
	}

	public boolean insertData(String type, int rootID, int pid, int taskID,
			String fromID, String toID, String content, List<Map> appendFile) {
		boolean isSuccess = true;

		String sql = "";
		Connection conn = ConnectDB.getConnection();
		boolean autoCommit;
		try {
			autoCommit = conn.getAutoCommit();
			conn.setAutoCommit(false);// 取消自动提交

			sql = "insert taskreport values(null, ?, ?, ?, '0', ?, ?, '1', now(), ?, '0', '0')";
			List<Object> tlist = new ArrayList<Object>();
			tlist.add(type);
			tlist.add(rootID);
			tlist.add(pid);
			tlist.add(taskID);
			System.out.println(taskID);
			tlist.add(fromID);
			tlist.add(content);
			OperateDB.setList(tlist);
			int tn = OperateDB.getGeneratedKey(sql);
			if (tn > 0) {
				if (isSuccess) {
					sql = "update taskreport set isleaf = '1' where t_reportid = "
							+ pid;
					if (OperateDB.update(sql) < 0) {
						isSuccess = false;
					}
				}

				if (isSuccess) {
					sql = "insert into taskmember values(?, ?)";
					List<Object> tmlist = new ArrayList<Object>();
					tmlist.add(tn);
					tmlist.add(toID);
					OperateDB.setList(tmlist);
					if (!OperateDB.insert(sql)) {
						isSuccess = false;
					}
				}

				if (isSuccess) {
					if (appendFile != null || appendFile.size() > 0) {
						for (int i = 0; i < appendFile.size(); i++) {
							Map<String, String> map = new HashMap<String, String>();
							map = (Map<String, String>) appendFile.get(i);
							String fileName = (String) map.get("fileName");
							String saveName = (String) map.get("saveName");
							// 向appendinfo表插入数据
							sql = "insert into appendinfo values('任务报告', ?, ?, ?)";
							List<Object> alist = new ArrayList<Object>();
							alist.add(tn);
							alist.add(fileName);
							alist.add(saveName);

							OperateDB.setList(alist);
							if (OperateDB.insert(sql)) {// 插入附件表是否成功
								sql = "update taskreport set isappend = '1' where t_reportid = "
										+ tn;
								if (OperateDB.update(sql) < 0) {
									isSuccess = false;
								}
							} else {
								isSuccess = false;
							}
						}// for结束
					}
				}

			} else {
				isSuccess = false;
			}

			if (isSuccess) {
				conn.commit();// 手动提交
			} else {
				conn.rollback();
			}
			conn.setAutoCommit(autoCommit);// 恢复现场
		} catch (SQLException e) {
			e.printStackTrace();
			isSuccess = false;
		} finally {
			OperateDB.close(conn);
		}

		return isSuccess;
	}
}
